﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

namespace Microsoft.WVB.Framework.AdControls
{
    /// <summary>
    /// An implementation of an <see cref="AdLayoutBase"/> class component.
    /// AdBanner is a control placeholder for Banner Ads. It has only one Area for displaying Ads
    /// but supports Images, Videos and Xap format.
    /// </summary>
    [TemplatePart(Name = AdBanner.AdAreaName, Type = typeof(ContentControl))]
    public sealed class AdBanner : AdLayoutBase
    {
        private const string AdAreaName = "AdArea";
        internal ContentControl AdArea = null;

        public AdBanner()
            : base()
        {
            this.DefaultStyleKey = typeof(AdBanner);
        }

        /// <summary>
        /// When overriden, provides a placeholder for developer to control actions to execute
        /// when the Ad is notifying it will stop.
        /// </summary>
        /// <remarks>Developer can override it when the AdBanner contains Video or Xap annimation.
        /// There is no real event being fired when the Banner is an image execept when a time duration
        /// is specified in the Ad from the Ad Provider.
        /// By default, OnAdStopped calls <see cref="OnEndShowAd"/>.
        /// </remarks>
        public override void OnAdStopped()
        {
            this.OnEndShowAd();
        }

        /// <summary>
        /// When overriden, provides a palceholder for developer to control actions to execute 
        /// when the Ad needs to be shown on the screen.
        /// </summary>
        public override void OnBeginShowAd()
        {
        }

        /// <summary>
        /// When overriden, provides a palceholder for developer to control actions to execute 
        /// when the Ad needs to diesappear from the screen.
        /// </summary>
        public override void OnEndShowAd()
        {
            AdArea = null;
        }

        /// <summary>
        /// Prepares the Ad to be displayed but does not show it into the UI.
        /// </summary>
        /// <param name="Ad">A <see cref="FrameowrkElement"/> that defines the Ad's UI to be position 
        /// in the VisualTree.</param>
        public override void OnBufferingAd(FrameworkElement Ad)
        {
            if (this.Context.DisplayRegion == AdOverlayMode.NotApplicable)
            {
                AdArea.Content = Ad;
                Ad.UpdateLayout();
            }
        }

        /// <summary>
        /// Builds the visual tree for the AdBanner when a new template is applied.
        /// Also, this is where internal logic hooks with UI elements part of the TemplatePart contract.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            AdArea = base.GetTemplateChild(AdBanner.AdAreaName) as ContentControl;
        }
    }
}
